package com.ys.fmms.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.builders.RequestParameterBuilder;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.service.RequestParameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger3配置信息
 */
@Configuration
@EnableOpenApi
public class Swagger3Config {
    // 返回文档摘要信息
    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .groupName("WebApi")
                .apiInfo(apiInfo())
                .enable(true)
                .select()
                // 包的方式
                .apis(RequestHandlerSelectors.basePackage("com.ys.fmms.controller"))
                // 方法注解
                // .apis(RequestHandlerSelectors.withMethodAnnotation(Operation.class))
                .build()
                .globalRequestParameters(getGlobalRequestParameters());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("野外监测管理系统接口文档")
                .description("本文档描述了各个接口的功能")
                .version("1.0")
                .contact(new Contact("ys", "http://localhost:9001/Swagger-ui/index.html", "shuai_168@qq.com"))
                .build();
    }

    private List<RequestParameter> getGlobalRequestParameters() {
        List<RequestParameter> parameterList = new ArrayList<>();
        parameterList.add(new RequestParameterBuilder()
                .name("token")
                .description("token")
                .required(false)
                .in(ParameterType.HEADER)
                .build());
        return parameterList;
    }
}
